package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.ApplyTcDao;
import com.gdit.zax.entitys.ApplyTcInfo;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.utils.DBUtil;

public class ApplyTcDaoImpl implements ApplyTcDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public ApplyTcDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	
	public boolean applytc(int uid,String truename, String identitycard, String content) {
		try {
			String sql = "insert t_applyteacher set UserId=?,TrueName=?,IdentityCard=?,Content=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setString(2, truename);
			ps.setString(3, identitycard);
			ps.setString(4, content);
			
			int result = ps.executeUpdate();
			if (result!=0) {
				sql = "update t_user set IdentityCard=? where Id=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, identitycard);
				ps.setInt(2, uid);
				ps.executeUpdate();
				
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public ApplyTcInfo getapplyinfo(int uid) {
		try {
			String sql = "select * from t_applyteacher where UserId=? group by AddTime desc limit 0,1";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			rs = ps.executeQuery();
			if (rs.next()) {
				ApplyTcInfo info = DBUtil.getobject(rs, ApplyTcInfo.class);				
				return info;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ApplyTcInfo getapplyinfo2(int aid) {
		try {
			String sql = "select * from t_applyteacher where Id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, aid);
			rs = ps.executeQuery();
			if (rs.next()) {
				ApplyTcInfo info = DBUtil.getobject(rs, ApplyTcInfo.class);				
				return info;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean isapplying(int uid) {
		try {
			String sql = "select * from t_applyteacher where UserId=? and Result=0 group by AddTime desc limit 0,1";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			rs = ps.executeQuery();
			if (rs.next()) {		
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean assessor(UserInfo uinfo,ApplyTcInfo ainfo) {
		if (DBUtil.saveobject(conn, "t_applyteacher", "Id = " + ainfo.getId(), ainfo)){
			uinfo.setUlevel(Constant.ULEVEL_teacher);
			uinfo.setIdentityCard(ainfo.getIdentitycard());
			if (DBUtil.saveobject(conn, "t_user", "Id=" + uinfo.getId(), uinfo)) {
				return true;
			}
		}
		return false;
	}
	
	public ArrayList<ApplyTcInfo> getAllApplyTc() {
		ArrayList<ApplyTcInfo> list = new ArrayList<ApplyTcInfo>();
		try {
			String sql = "select * from t_applyteacher where Result=0";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				ApplyTcInfo info = DBUtil.getobject(rs, ApplyTcInfo.class);				
				list.add(info);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<ApplyTcInfo> getApplyTc(int page, int onepagecount) {
		ArrayList<ApplyTcInfo> list = new ArrayList<ApplyTcInfo>();
		try {
			String sql = "select * from t_applyteacher where Result=0 limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*onepagecount);
			ps.setInt(2, onepagecount);
			rs = ps.executeQuery();
			while (rs.next()) {
				ApplyTcInfo info = DBUtil.getobject(rs, ApplyTcInfo.class);				
				list.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
